package com.sijia.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sijia.reggie.comment.BaseContext;
import com.sijia.reggie.entity.*;
import com.sijia.reggie.mapper.OrderMapper;
import com.sijia.reggie.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @author 杨思佳
 * @version 1.0
 */

@Service
public class OrderServiceImpl
        extends ServiceImpl<OrderMapper, Orders> implements OrderService {

    @Autowired
    private AddressBookService abs;

    @Autowired
    private ShoppingCartService scs;

    @Autowired
    private OrderDetailService ods;

    @Autowired
    private UserService us;
    


    @Override
    public void saveOrder(Orders orders) {
        AtomicInteger atomicInteger = new AtomicInteger(0);//原子整形数，避免在多线程高并发的条件下出现数据异常
        Long addressBookId = orders.getAddressBookId();
        orders.setId(IdWorker.getId());
        orders.setNumber(String.valueOf(IdWorker.getId()));
        LambdaQueryWrapper<AddressBook> bookLambdaQueryWrapper = new LambdaQueryWrapper<>();
        bookLambdaQueryWrapper.eq(AddressBook::getId, addressBookId);
        AddressBook addressBook = abs.getOne(bookLambdaQueryWrapper);
        orders.setAddress(addressBook.getDetail());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setConsignee(addressBook.getConsignee());
        orders.setOrderTime(LocalDateTime.now());
        orders.setPhone(addressBook.getPhone());
        orders.setStatus(1);
        orders.setUserId(BaseContext.getCurrentId());
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCarts = scs.list(queryWrapper);
        List<OrderDetail> orderDetails = shoppingCarts.stream().map(item -> {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setAmount(item.getAmount());
            orderDetail.setDishFlavor(item.getDishFlavor());
            orderDetail.setDishId(item.getDishId());
            orderDetail.setImage(item.getImage());
            orderDetail.setName(item.getName());
            orderDetail.setNumber(item.getNumber());
            orderDetail.setOrderId(orders.getId());
            orderDetail.setSetmealId(item.getSetmealId());
            atomicInteger.getAndAdd(item.getAmount().multiply(new BigDecimal(item.getNumber())).intValue());
            return orderDetail;
        }).collect(Collectors.toList());
        orders.setAmount(new BigDecimal(atomicInteger.get()));
        User user = us.getById(BaseContext.getCurrentId());
        orders.setUserName(user.getName());
        this.save(orders);
        ods.saveBatch(orderDetails);
        scs.clean();
    }
}
